package com.framework.dubbo.producer.invoke.impl;

import com.framework.dubbo.producer.invoke.InvokeCommand;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

/**
 * 描述：
 *  线程命令直接调用
 *  直接调用，有错误信息不容易打印出来 不建议使用
 * @author xianggj
 * @Date 2021/8/25 10:37
 **/
@Component("runtime")
public class RuntimeInvokeCommand implements InvokeCommand {

    @Override
    public Map<String, Throwable> invokeCommand(File file, String command, CountDownLatch latch)  throws Throwable{
        // 处理失败的pdf统计
        Map<String, Throwable> failedMap = new ConcurrentHashMap(10);
        try {
            //这里面的详细报错打印不出来
            Runtime.getRuntime().exec(command);
        } catch (Exception e) {
            e.printStackTrace();
            failedMap.put(file.getName() , e);
        } finally {
            if (latch != null) {
                latch.countDown();
            }
        }
        return failedMap;
    }

    @Override
    public Map<String, Object> getMetric() {
        return new HashMap<>();
    }
}
